import os

from ament_index_python.packages import get_package_share_directory

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, SetEnvironmentVariable
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description():
    # firstly getting the uart node
    # in this node, we need to solve control_id
    hnurm_uart_dir = get_package_share_directory('hnurm_uart')
    params_file_uart = LaunchConfiguration('uart_params_file')
    control_id = LaunchConfiguration('control_id')

    hnurm_uart_ld = LaunchDescription([
        DeclareLaunchArgument(
            'uart_params_file',
            default_value=os.path.join(hnurm_uart_dir, 'params', 'default.yaml'),
            description='uart params file'
        ),
        DeclareLaunchArgument(
            'control_id',
            default_value="1.0",
            description='control id'
        ),
        Node(
            package='hnurm_uart',
            executable='hnurm_uart_node',
            output='screen',
            parameters=[params_file_uart, {'control_id': control_id}]
        ),
    ])

    # getting the camera node
    # should tell which camera to open
    # so camera_id param should be rewritten
    hnurm_camera_dir = get_package_share_directory('hnurm_camera')
    params_file_camera = LaunchConfiguration('camera_params_file')

    hnurm_camera_ld = LaunchDescription([
        DeclareLaunchArgument(
            'camera_params_file',
            default_value=os.path.join(hnurm_camera_dir, 'params', 'default.yaml'),
            description='camera params file'
        ),
        Node(
            package='hnurm_camera',
            executable='hnurm_camera_node',
            output='screen',
            parameters=[params_file_camera]
        )
    ])

    # now return the launch description
    return LaunchDescription([
        hnurm_uart_ld,
        hnurm_camera_ld,
    ])
